﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace RflightPimTestInstr
{
    class PimPort
    {
        public string sign;
        public bool check;
    }

    class PimBand
    {
        public string sign;
        public bool check;

        public List<PimPort> portsList;

        public int portIndex;
        public int order;

        public double f1Min;
        public double f1Max;

        public double f2Min;
        public double f2Max;

        public double imfMin;
        public double imfMax;

        public double f1;
        public double f2;
        public double imf;

        public double startFreq;
        public double stopFreq;
        public double step;

        public double f1Start;
        public double f1Stop;

        public double f2Start;
        public double f2Stop;

        public double imfStart;
        public double imfStop;

        public double p1;
        public double p2;

        public double off1;
        public double off2;
        public double imOff;
    }

    class PimInstr
    {
        public string ipAddr;
        public string tcpPort;
        public string sign;
        public bool check;

        public string model;
        public string snum;

        public double powerMin;
        public double powerMax;

        public int bandIndex;

        public bool outp1;
        public bool outp2;

        public double lev1;
        public double lev2;
        public double iml;

        public byte alarm;

        public List<PimBand> bandsList;

        private TcpClient client;
        private NetworkStream netStream;

        public bool Open()
        {
            client = new TcpClient();
            try
            {
                client.Connect(ipAddr, int.Parse(tcpPort));
            }
            catch (System.Net.Sockets.SocketException)
            {
                return false;
            }

            client.SendTimeout = 1000;
            client.ReceiveTimeout = 1000;

            netStream = client.GetStream();

            return true;
        }

        public void Close()
        {
            if (client == null)
            {
                return;
            }

            if (netStream == null)
            {
                return;
            }

            netStream.Close();
            netStream = null;
            client.Close();
            client = null;
        }

        public bool Send(string buf)
        {
            byte[] sendBytes = Encoding.UTF8.GetBytes(buf);
            for (int count = sendBytes.Length; count > 0; )
            {
                try
                {
                    if (netStream.CanWrite)
                    {
                        if (count <= client.SendBufferSize)
                        {
                            netStream.Write(sendBytes, sendBytes.Length - count, count);
                            count = 0;
                            break;
                        }
                        else
                        {
                            netStream.Write(sendBytes, sendBytes.Length - count, client.SendBufferSize);
                            count -= client.SendBufferSize;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }

            return true;
        }

        public int Recv(out string buf, int length)
        {
            buf = null;
            int result = 0, value;

            byte[] recivebytes = new byte[client.ReceiveBufferSize];
            while (true)
            {
                try
                {
                    if (netStream.CanRead)
                    {
                        Array.Clear(recivebytes, 0, recivebytes.Length);
                        value = netStream.Read(recivebytes, 0, (int)client.ReceiveBufferSize);
                        if (value > 0)
                        {
                            result += value;
                            buf += Encoding.UTF8.GetString(recivebytes).TrimEnd('\0');
                        }

                        if (result >= length)
                        {
                            break;
                        }

                        if (recivebytes[value - 1] == '\n')
                        {
                            break;
                        }
                    }
                }
                catch
                {
                    break;
                }
            }

            return result;
        }

        public bool GetIDN()
        {            
            if(!Send("*IDN?\n"))
            {
                return false;
            }

            string rbuf = null;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            string[] idn = rbuf.Split(',');
            if (idn.Length < 3)
            {
                return false;
            }

            if(idn[0] != "Rflight")
            {
                return false;
            }

            model = idn[1];
            snum = idn[2];

            return true;
        }

        public bool GetCarrierPowerRange(ref double min, ref double max)
        {
            if (!Send("CARR:POW:LIM?\n"))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            string[] para = rbuf.Split(',');
            if (para.Length != 2)
            {
                return false;
            }

            try
            {
                min = Convert.ToDouble(para[0]);
            }
            catch
            {
                return false;
            }

            try
            {
                max = Convert.ToDouble(para[1]);
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool GetBandSize(int band, ref int num)
        {
            if (!Send("BAND:SIZE?\n"))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            try
            {
                num = Convert.ToInt32(rbuf);
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool GetBandArgs(int band, PimBand bandItem)
        {
            string tbuf = String.Format("BAND{0}:LIM?\n", band);
            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            string[] para = rbuf.Split(',');
            if (para.Length != 6)
            {
                return false;
            }

            double freq;
            for (int index = 0; index < 6; index++)
            {
                try
                {
                    freq = Convert.ToDouble(para[index]);
                }
                catch
                {
                    return false;
                }

                switch (index)
                {
                    case 0:
                        bandItem.f1Min = freq;
                        break;
                    case 1:
                        bandItem.f1Max = freq;
                        break;
                    case 2:
                        bandItem.f2Min = freq;
                        break;
                    case 3:
                        bandItem.f2Max = freq;
                        break;
                    case 4:
                        bandItem.imfMin = freq;
                        break;
                    case 5:
                        bandItem.imfMax = freq;
                        break;
                }

            }

            return true;
        }

        public bool GetParameter()
        {
            int num = 0;
            if(!GetBandSize(0, ref num))
            {
                return false;
            }

            bandsList = new List<PimBand>();
            for (int index = 0; index < num; index++)
            {
                PimBand bandItem = new PimBand();
                if(!GetBandArgs(index, bandItem))
                {
                    return false;
                }

                bandsList.Add(bandItem);
            }

            if (!GetCarrierPowerRange(ref powerMin, ref powerMax))
            {
                return false;
            }

            return true;
        }

        public bool SetCarrierFreq(int source, double freq)
        {
            string tbuf = String.Format("CARR{0}:FREQ {1:000.000} MHz\n", source, freq);

            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            if (rbuf != "ACK\n")
            {
                return false;
            }

            return true;
        }

        public bool SetCarrierAtt(int source, double att)
        {
            string tbuf = String.Format("CARR{0}:ATT {1:00.00} dB\n", source, att);

            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            if (rbuf != "ACK\n")
            {
                return false;
            }

            return true;
        }

        public bool SetBand(int index)
        {
            string tbuf = String.Format("FREQ:BAND {0}\n", index);

            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            if (rbuf != "ACK\n")
            {
                return false;
            }

            return true;
        }

        public bool SetPort(int port)
        {
            string tbuf = String.Format("SYST:MODE {0}\n", port);

            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            if (rbuf != "ACK\n")
            {
                return false;
            }

            return true;
        }

        public bool SetOrder(int order)
        {
            string tbuf = String.Format("IM:ORD {0}\n", order);

            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            if (rbuf != "ACK\n")
            {
                return false;
            }

            return true;
        }

        public bool SetImfRange(double min, double max)
        {
            string tbuf = null;

            for (int index = 0; index < 2; index++)
            {
                if (index == 0)
                {
                    tbuf = String.Format("IM:FREQ:STAR {0:000.000} MHz\n", min);
                }
                else
                {
                    tbuf = String.Format("IM:FREQ:STOP {0:000.000} MHz\n", max);
                }
                
                if (!Send(tbuf))
                {
                    return false;
                }

                string rbuf;
                if (0 == Recv(out rbuf, 1024))
                {
                    return false;
                }

                if (rbuf != "ACK\n")
                {
                    return false;
                }
            }
            return true;
        }

        public bool SetCarrierOutp(int source, bool status)
        {
            if (client == null)
            {
                return true;
            }

            if (netStream == null)
            {
                return true;
            }

            string outStat = null;
            if (status)
            {
                outStat = "ON";
            }
            else
            {
                outStat = "OFF";
            }

            string tbuf = String.Format("CARR{0}:OUTP:STAT {1}\n", source, outStat);

            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            if (rbuf != "ACK\n")
            {
                return false;
            }

            return true;
        }

        public bool QueryCarrierAtt(int source, ref double att)
        {
            string tbuf = String.Format("CARR{0}:ATT?\n", source);
            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            try
            {
                string[] reply = rbuf.Split(' ');
                att = Convert.ToDouble(reply[0]);
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool QueryCarrierLevel(int source, ref double level)
        {
            string tbuf = String.Format("CARR{0}:POW?\n", source);
            if (!Send(tbuf))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            try
            {
                string[] reply = rbuf.Split(' ');
                level = Convert.ToDouble(reply[0]);
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool QueryImLevel()
        {
            if (!Send("IM:POW?\n"))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            try
            {
                string[] reply = rbuf.Split(' ');
                iml = Convert.ToDouble(reply[0]);
            }
            catch
            {
                return false;
            }

            iml += bandsList[bandIndex].imOff;

            return true;
        }

        public bool QueryAlarmStatus()
        {
            byte status;


            if (!Send("ALAR:STAT?\n"))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            if(rbuf.Length != 9)
            {
                return false;
            }

            status = 0;
            for (int index = 0; index < 8; index++)
            {
                if ((rbuf[index] > 0x31) || (rbuf[index] < 0x30))
                {
                    return false;
                }

                if (rbuf[index] == 0x31)
                {
                    status |= (byte)(0x01 << (7 - index));
                }
            }

            alarm = status;
            return true;
        }

        public double GetIMFreq(int index)
        {
            int factor1, factor2;

            if (bandsList[index].order == 2)
            {
                return bandsList[index].imf = bandsList[index].f1 + bandsList[index].f2;
            }

            factor1 = (bandsList[index].order + 1) / 2;
            factor2 = factor1 - 1;

            if (bandsList[index].startFreq > bandsList[index].imfStart)
            {
                return bandsList[index].imf = factor1 * bandsList[index].f1 - factor2 * bandsList[index].f2;
            }
            else
            {
                return bandsList[index].imf = factor1 * bandsList[index].f2 - factor2 * bandsList[index].f1;
            }
        }

        public double GetCarrer1Freq(int index, double imf)
        {
            int factor1, factor2;

            if (bandsList[index].order == 2)
            {
                return imf - bandsList[index].stopFreq;
            }

            factor1 = (bandsList[index].order + 1) / 2;
            factor2 = factor1 - 1;

            if (bandsList[index].startFreq > bandsList[index].imfStart)
            {
                return (imf + factor2 * bandsList[index].stopFreq) / factor1;
            }
            else
            {
                return  (factor1 * bandsList[index].stopFreq - imf) / factor2;
            }
        }

        public double GetCarrer2Freq(int index, double imf)
        {
            int factor1, factor2;

            if (bandsList[index].order == 2)
            {
                return imf - bandsList[index].startFreq;
            }

            factor1 = (bandsList[index].order + 1) / 2;
            factor2 = factor1 - 1;

            if (bandsList[index].startFreq > bandsList[index].imfStart)
            {
                return (factor1 * bandsList[index].startFreq - imf) / factor2;
            }
            else
            {
                return (imf + factor2 * bandsList[index].startFreq) / factor1;
            }
        }

        public bool GeIMFSweepRang(int index, ref double min, ref double max)
        {
            int factor1, factor2;
            double freq;

            if (bandsList[index].order == 2)
            {
                min = bandsList[index].f1Start + bandsList[index].stopFreq;
                freq = bandsList[index].startFreq + bandsList[index].f2Stop;
                if (min > freq)
                {
                    min = freq;
                }

                max = bandsList[index].f1Stop + bandsList[index].stopFreq;
                freq = bandsList[index].startFreq + bandsList[index].f2Start;
                if (max < freq)
                {
                    max = freq;
                }

                return true;
            }

            factor1 = (bandsList[index].order + 1) / 2;
            factor2 = factor1 - 1;
            if (bandsList[index].startFreq > bandsList[index].imfStart)
            {
                min = factor1 * bandsList[index].f1Start - factor2 * bandsList[index].stopFreq;
                freq = factor1 * bandsList[index].startFreq - factor2 * bandsList[index].f2Start;
                if (min > freq)
                {
                    min = freq;
                }

                max = factor1 * bandsList[index].f1Stop - factor2 * bandsList[index].stopFreq;
                freq = factor1 * bandsList[index].startFreq - factor2 * bandsList[index].f2Stop;
                if (max < freq)
                {
                    max = freq;
                }
            }
            else
            {
                min = factor1 * bandsList[index].stopFreq - factor2 * bandsList[index].f1Start;
                freq = factor1 * bandsList[index].f2Start - factor2 * bandsList[index].startFreq;
                if (min > freq)
                {
                    min = freq;
                }

                max = factor1 * bandsList[index].stopFreq - factor2 * bandsList[index].f1Stop;
                freq = factor1 * bandsList[index].f2Stop - factor2 * bandsList[index].startFreq;
                if (max < freq)
                {
                    max = freq;
                }
            }

            return true;
        }

        public bool GetF1SweepRang(int index)
        {
            int factor1, factor2;
            double freq, step, imf;

            if (bandsList[index].order == 2)
            {
                freq = bandsList[index].startFreq;
                step = bandsList[index].step;

                while ((freq <= bandsList[index].f1Max) && (freq < bandsList[index].stopFreq))
                {
                    imf = freq + bandsList[index].stopFreq;
                    if ((imf >= bandsList[index].imfStart) && (imf <= bandsList[index].imfStop))
                    {
                        bandsList[index].f1Start = freq;
                        break;
                    }

                    freq += step;
                }

                if ((freq > bandsList[index].f1Max) || (freq >= bandsList[index].stopFreq))
                {
                    return false;
                }

                while ((freq <= bandsList[index].f1Max) && (freq < bandsList[index].stopFreq))
                {
                    imf = freq + bandsList[index].stopFreq;
                    if (imf == bandsList[index].imfStop)
                    {
                        bandsList[index].f1Stop = freq;
                        return true;
                    }
                    else if (imf > bandsList[index].imfStop)
                    {
                        bandsList[index].f1Stop = freq - step;
                        return true;
                    }

                    freq += step;
                }

                bandsList[index].f1Stop = freq - step;

                return true;
            }

            factor1 = (bandsList[index].order + 1) / 2;
            factor2 = factor1 - 1;

            if (bandsList[index].startFreq > bandsList[index].imfStart)
            {
                freq = bandsList[index].startFreq;
                step = bandsList[index].step / factor1;

                while ((freq <= bandsList[index].f1Max) && (freq < bandsList[index].stopFreq))
                {
                    imf = factor1 * freq - factor2 * bandsList[index].stopFreq;

                    if ((imf >= bandsList[index].imfStart) && (imf <= bandsList[index].imfStop))
                    {
                        bandsList[index].f1Start = freq;
                        break;
                    }

                    freq += step;
                }

                if ((freq > bandsList[index].f1Max) || (freq >= bandsList[index].stopFreq))
                {
                    return false;
                }

                while ((freq <= bandsList[index].f1Max) && (freq < bandsList[index].stopFreq))
                {
                    imf = factor1 * freq - factor2 * bandsList[index].stopFreq;

                    if (imf == bandsList[index].imfStop)
                    {
                        bandsList[index].f1Stop = freq;
                        return true;
                    }
                    else if (imf > bandsList[index].imfStop)
                    {
                        bandsList[index].f1Stop = freq - step;
                        return true;
                    }

                    freq += step;
                }

                bandsList[index].f1Stop = freq - step;
            }
            else
            {
                freq = bandsList[index].startFreq;
                step = bandsList[index].step / factor2;

                while ((freq <= bandsList[index].f1Max) && (freq < bandsList[index].stopFreq))
                {
                    imf = factor1 * bandsList[index].stopFreq - factor2 * freq;

                    if ((imf >= bandsList[index].imfStart) && (imf <= bandsList[index].imfStop))
                    {
                        bandsList[index].f1Stop = freq;
                        break;
                    }

                    freq += step;
                }

                if ((freq > bandsList[index].f1Max) || (freq >= bandsList[index].stopFreq))
                {
                    return false;
                }

                while ((freq <= bandsList[index].f1Max) && (freq < bandsList[index].stopFreq))
                {
                    imf = factor1 * bandsList[index].stopFreq - factor2 * freq;

                    if (imf == bandsList[index].imfStart)
                    {
                        bandsList[index].f1Start = freq;
                        return true;
                    }
                    else if (imf < bandsList[index].imfStart)
                    {
                        bandsList[index].f1Start = freq - step;
                        return true;
                    }

                    freq += step;
                }

                bandsList[index].f1Start = freq - step;
            }
            
            return true;
        }

        public bool GetF2SweepRang(int index)
        {
            int factor1, factor2;
            double freq, step, imf;


            if (bandsList[index].order == 2)
            {
                freq = bandsList[index].stopFreq;
                step = bandsList[index].step;

                while ((freq >= bandsList[index].f2Min) && (freq > bandsList[index].startFreq))
                {
                    imf = bandsList[index].startFreq + freq;
                    if ((imf >= bandsList[index].imfStart) && (imf <= bandsList[index].imfStop))
                    {
                        bandsList[index].f2Start = freq;
                        break;
                    }

                    freq -= step;
                }

                if ((freq < bandsList[index].f2Min) || (freq <= bandsList[index].startFreq))
                {
                    return false;
                }

                while ((freq >= bandsList[index].f2Min) && (freq > bandsList[index].startFreq))
                {
                    imf = bandsList[index].startFreq + freq;
                    if (imf == bandsList[index].imfStop)
                    {
                        bandsList[index].f2Stop = freq;
                        return true;
                    }
                    else if (imf > bandsList[index].imfStop)
                    {
                        bandsList[index].f2Stop = freq + step;
                        return true;
                    }

                    freq -= step;
                }

                bandsList[index].f2Stop = freq + step;
                return true;
            }

            factor1 = (bandsList[index].order + 1) / 2;
            factor2 = factor1 - 1;

            if (bandsList[index].startFreq > bandsList[index].imfStart)
            {
                freq = bandsList[index].stopFreq;
                step = bandsList[index].step / factor2;

                while ((freq >= bandsList[index].f2Min) && (freq > bandsList[index].startFreq))
                {
                    imf = factor1 * bandsList[index].startFreq - factor2 * freq;

                    if ((imf >= bandsList[index].imfStart) && (imf <= bandsList[index].imfStop))
                    {
                        bandsList[index].f2Start = freq;
                        break;
                    }

                    freq -= step;
                }

                if ((freq < bandsList[index].f2Min) || (freq <= bandsList[index].startFreq))
                {
                    return false;
                }

                while ((freq >= bandsList[index].f2Min) && (freq > bandsList[index].startFreq))
                {
                    imf = factor1 * bandsList[index].startFreq - factor2 * freq;

                    if (imf == bandsList[index].imfStop)
                    {
                        bandsList[index].f2Stop = freq;
                        return true;
                    }
                    else if (imf > bandsList[index].imfStop)
                    {
                        bandsList[index].f2Stop = freq + step;
                        return true;
                    }

                    freq -= step;
                }

                bandsList[index].f2Stop = freq + step;
            }
            else
            {
                freq = bandsList[index].stopFreq;
                step = bandsList[index].step / factor1;

                while ((freq >= bandsList[index].f2Min) && (freq > bandsList[index].startFreq))
                {
                    imf = factor1 * freq - factor2 * bandsList[index].startFreq;

                    if ((imf >= bandsList[index].imfStart) && (imf <= bandsList[index].imfStop))
                    {
                        bandsList[index].f2Stop = freq;
                        break;
                    }

                    freq -= step;
                }

                if ((freq < bandsList[index].f2Min) || (freq <= bandsList[index].startFreq))
                {
                    return false;
                }

                while ((freq >= bandsList[index].f2Min) && (freq > bandsList[index].startFreq))
                {
                    imf = factor1 * freq - factor2 * bandsList[index].startFreq;

                    if (imf == bandsList[index].imfStart)
                    {
                        bandsList[index].f2Start = freq;
                        return true;
                    }
                    else if (imf < bandsList[index].imfStart)
                    {
                        bandsList[index].f2Start = freq + step;
                        return true;
                    }

                    freq -= step;
                }

                bandsList[index].f2Start = freq + step;
            }

            return true;
        }

        //public bool ShiftSweepFreq(int index, ref SweepWay way)
        //{
        //    int factor1, factor2;
        //    double step, freq;
        //    bool over = false;


        //    if (bandsList[index].order == 2)
        //    {
        //        if (way == SweepWay.Up)
        //        {
        //            step = bandsList[index].step;
        //            freq = bandsList[index].f1 + step;
        //            if (freq <= bandsList[index].f1Stop)
        //            {
        //                bandsList[index].f1 = freq;
        //            }
        //            else
        //            {
        //                bandsList[index].f1 = bandsList[index].startFreq;
        //                bandsList[index].f2 = bandsList[index].f2Start;
        //                way = SweepWay.Down;
        //            }
        //        }
        //        else
        //        {
        //            step = bandsList[index].step;
        //            freq = bandsList[index].f2 - step;
        //            if (freq >= bandsList[index].f2Stop)
        //            {
        //                bandsList[index].f2 = freq;
        //            }
        //            else
        //            {
        //                bandsList[index].f1 = bandsList[index].f1Start;
        //                bandsList[index].f2 = bandsList[index].stopFreq;
        //                way = SweepWay.Up;
        //            }
        //        }

        //        bandsList[index].imf = bandsList[index].f1 + bandsList[index].f2;
        //        return true;
        //    }

        //    factor1 = (bandsList[index].order + 1) / 2;
        //    factor2 = factor1 - 1;

        //    if (bandsList[index].startFreq > bandsList[index].imfStart)
        //    {
        //        if (way == SweepWay.Up)
        //        {
        //            step = bandsList[index].step / factor1;
        //            freq = bandsList[index].f1 + step;
        //            if (freq <= bandsList[index].f1Stop)
        //            {
        //                bandsList[index].f1 = freq;
        //            }
        //            else
        //            {
        //                bandsList[index].f1 = bandsList[index].startFreq;
        //                bandsList[index].f2 = bandsList[index].f2Start;
        //                way = SweepWay.Down;                       
        //            }
        //        }
        //        else
        //        {
        //            step = bandsList[index].step / factor2;
        //            freq = bandsList[index].f2 - step;
        //            if (freq >= bandsList[index].f2Stop)
        //            {
        //                bandsList[index].f2 = freq;
        //            }
        //            else
        //            {
        //                bandsList[index].f1 = bandsList[index].f1Start;
        //                bandsList[index].f2 = bandsList[index].stopFreq;
        //                way = SweepWay.Up;
        //                over = true;
        //            }
        //        }

        //        bandsList[index].imf = factor1 * bandsList[index].f1 - factor2 * bandsList[index].f2;
        //    }
        //    else
        //    {
        //        if (way == SweepWay.Up)
        //        {
        //            step = bandsList[index].step / factor2;
        //            freq = bandsList[index].f1 - step;
        //            if (freq >= bandsList[index].f1Stop)
        //            {
        //                bandsList[index].f1 = freq;
        //            }
        //            else
        //            {
        //                bandsList[index].f1 = bandsList[index].startFreq;
        //                bandsList[index].f2 = bandsList[index].f2Start;
        //                way = SweepWay.Down;
        //            }
        //        }
        //        else
        //        {
        //            step = bandsList[index].step / factor1;
        //            freq = bandsList[index].f2 + step;
        //            if (freq <= bandsList[index].f2Stop)
        //            {
        //                bandsList[index].f2 = freq;
        //            }
        //            else
        //            {
        //                bandsList[index].f1 = bandsList[index].f1Start;
        //                bandsList[index].f2 = bandsList[index].stopFreq;
        //                way = SweepWay.Up;
        //                over = true;
        //            }
        //        }

        //        bandsList[index].imf = factor1 * bandsList[index].f2 - factor2 * bandsList[index].f1;
        //    }

        //    return over;
        //}

        public bool IncreaseCarrierAtt(int source)
        {
            double att = 31.75;


            if (!QueryCarrierAtt(source, ref att))
            {
                return false;
            }

            if (!SetCarrierAtt(source, att + 1))
            {
                return false;
            }
            return true;
        }

        public bool Carrier1Leveling()
        {
            double att = 31.75, delta = 0;
            int cycle = 3;

            for (int loop = 0; loop < cycle; loop++ )
            {
                if (!outp1)
                {
                    return true;
                }

                if (!QueryAlarmStatus())
                {
                    return false;
                }

                if (!QueryCarrierLevel(1, ref lev1))
                {
                    return false;
                }

                delta = bandsList[bandIndex].p1 - lev1;
                if (Math.Abs(delta) <= 0.35)
                {
                    return true;
                }

                if (loop == 0)
                {
                    if(!QueryCarrierAtt(1, ref att))
                    {
                        return false;
                    }

                    cycle = (int)Math.Abs(delta)/2;
                    cycle += 4;
                }

                if (Math.Abs(delta) >= 2)
                {
                    if (delta > 0)
                    {
                        delta = 2.0;
                    }
                    else
                    {
                        delta = -2.0;
                    }
                }
                else if (Math.Abs(delta) > 1)
                {
                    if (delta > 0)
                    {
                        delta = 1.0;
                    }
                    else
                    {
                        delta = -1.0;
                    }
                }
                else if (Math.Abs(delta) > 0.5)
                {
                    if (delta > 0)
                    {
                        delta = 0.5;
                    }
                    else
                    {
                        delta = -0.5;
                    }
                }
                else
                {
                    if (delta > 0)
                    {
                        delta = 0.25;
                    }
                    else
                    {
                        delta = -0.25;
                    }
                }

                att -= delta;
                att = (int)(att * 100) / 25 * 25;
                att /= 100;
                if (att < 0)
                {
                    att = 0;
                    return true;
                }
                if (!SetCarrierAtt(1, att))
                {
                    return false;
                }

                Thread.Sleep(20);
            }

            if (Math.Abs(bandsList[bandIndex].p1 - lev1) >= 1.0)
            {
                //return false;
            }

            return true;
        }

        public bool Carrier2Leveling()
        {
            double att = 31.75, delta = 0;
            int cycle = 3;

            for (int loop = 0; loop < cycle; loop++)
            {
                if (!outp2)
                {
                    return true;
                }

                if (!QueryAlarmStatus())
                {
                    return false;
                }

                if (!QueryCarrierLevel(2, ref lev2))
                {
                    return false;
                }

                delta = bandsList[bandIndex].p2 - lev2;
                if (Math.Abs(delta) <= 0.35)
                {
                    return true;
                }

                if (loop == 0)
                {
                    if (!QueryCarrierAtt(2, ref att))
                    {
                        return false;
                    }

                    cycle = (int)Math.Abs(delta)/2;
                    cycle += 4;
                }

                if (Math.Abs(delta) >= 2)
                {
                    if (delta > 0)
                    {
                        delta = 2.0;
                    }
                    else
                    {
                        delta = -2.0;
                    }
                }
                else if (Math.Abs(delta) > 1)
                {
                    if (delta > 0)
                    {
                        delta = 1.0;
                    }
                    else
                    {
                        delta = -1.0;
                    }
                }
                else if (Math.Abs(delta) > 0.5)
                {
                    if (delta > 0)
                    {
                        delta = 0.5;
                    }
                    else
                    {
                        delta = -0.5;
                    }
                }
                else
                {
                    if (delta > 0)
                    {
                        delta = 0.25;
                    }
                    else
                    {
                        delta = -0.25;
                    }
                }

                att -= delta;
                att = (int)(att * 100) / 25 * 25;
                att /= 100;
                if (att < 0)
                {
                    att = 0;
                    return true;
                }
                if (!SetCarrierAtt(2, att))
                {
                    return false;
                }

                Thread.Sleep(20);
            }

            if (Math.Abs(bandsList[bandIndex].p2 - lev2) >= 1.0)
            {
                //return false;
            }

            return true;
        }

        public bool Reset()
        {
            if (!Send("*RST\n"))
            {
                return false;
            }

            string rbuf;
            if (0 == Recv(out rbuf, 1024))
            {
                return false;
            }

            if (rbuf != "ACK\n")
            {
                return false;
            }

            return true;
        }

        public bool Init()
        {
            if (!Reset())
            {
                return false;
            }

            if(!SetBand(bandIndex))
            {
                return false;
            }

            if (!SetPort(bandsList[bandIndex].portIndex))
            {
                return false;
            }

            if (!SetImfRange(bandsList[bandIndex].imfStart, bandsList[bandIndex].imfStop))
            {
                return false;
            }

            if (!SetOrder(bandsList[bandIndex].order))
            {
                return false;
            }

            if (!SetCarrierFreq(1, bandsList[bandIndex].f1))
            {
                return false;
            }

            if (!SetCarrierFreq(2, bandsList[bandIndex].f2))
            {
                return false;
            }

            if (!SetCarrierAtt(1, 15))
            {
                return false;
            }

            if (!SetCarrierAtt(2, 15))
            {
                return false;
            }

            return true;
        }

    }
}
